﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Twitter.offline
{
    public class Util
    {

        public Member getMemberByName(String name)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();
            return (from m in context.MemberJeu
                         where m.login == name
                         select m).First<Member>();
 
        }

        public IEnumerable<User> findAllUserPersisted(Member member)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();//)
            {
                IEnumerable<User> users = (from t in context.TweetJeu
                                           where t.Member.ID == member.ID
                                           select t.User).ToList<User>().Distinct();
                return users;
            }
        }

        public IEnumerable<Tweet> findAllUserTweet(User user)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();
            {
                IEnumerable<Tweet> tweets = (from t in context.TweetJeu
                                           where t.User.ID == user.ID
                                           select t).ToList<Tweet>();
                return tweets;
            }
        
        }

        public void deleteTweet(Tweet tweet)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();
            Tweet tw = (from t in context.TweetJeu
                        where t.ID == tweet.ID
                        select t).First<Tweet>();
            context.TweetJeu.DeleteObject(tw);
            context.SaveChanges();
        
        }

        public void deleteUserTweet(User user, Member member)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();
            IEnumerable<Tweet> tweets = (from t in context.TweetJeu
                                         where t.User.ID == user.ID
                                            &&
                                               t.Member.ID == member.ID
                                         select t).ToList<Tweet>();

            foreach (Tweet t in tweets)
                context.TweetJeu.DeleteObject(t);
            context.SaveChanges();
        }

        public void modifyTweet(String tweetId, String text)
        {
            TwitterEntitiesModelContainer context = new TwitterEntitiesModelContainer();
            long id = long.Parse(tweetId);

            Tweet tweets = (from t in context.TweetJeu
                                         where t.ID == id

                                         select t).FirstOrDefault<Tweet>();
            if (tweets == null)
                return;
            tweets.text = text;
            context.TweetJeu.ApplyCurrentValues(tweets);
            context.SaveChanges();
        
        }

    }
}